import { addExportTemplateApi, updateExportTemplateApi } from "@/api/configCenter/templateLibrary/index";
import { message } from "ant-design-vue";

export default function OperationHook(state: Base.GeneraDataType) {
  const { formState, formRef, emit, dataList, type: initValue, passData } = state;

  const onSubmit = () => {
    formRef.value.validate().then(() => {
      const params = {
        ...formState
      };
      const result: any = [];
      const nameArr: any = [];
      dataList.value.map((t: any) => {
        t.feilds.map((q: any) => {
          if (t.checkValue.includes(q.code)) {
            result.push(q.code);
            nameArr.push(q.name);
          }
        });
      });
      params.fieldList = result;
      params.nameList = nameArr;
      if (!params.fieldList.length) return message.error("请选择导出内容");
      let request;
      if (initValue.value === 1) {
        //新增
        request = addExportTemplateApi;
      } else {
        request = updateExportTemplateApi;
        params.id = passData.value.id;
      }
      request(params).then((res: any) => {
        const { code } = res;
        if (code === 200) {
          message.success("操作成功!");
          emit("submit");
        }
      });
    });
  };
  const resetForm = () => {
    formRef.value.resetFields();
    emit("submit");
  };

  const changeName = (e: string, option: any) => {
    formState.type = option.label;
  };
  return {
    resetForm,
    onSubmit,
    changeName
  };
}
